﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;

namespace zlsoft.BHExtend.Interface
{
   public class StringToText
    {
        /// <summary>
        /// 将字符串保存在文本文件中
        /// 作者：罗春生
        /// 日期：2018-10-08
        /// </summary>
        /// <param name="str">需要保存的文本字符</param>
        /// <param name="FileExtension">指定的文件后缀名</param>
        /// <returns></returns>
        public static string writtxt(string str,string FileExtension)
        {
            //用户选择保存路径以及文件名称
            string userpath = string.Empty;
            string tmppath = string.Empty;   
            string pathHZ = string.Empty;     
            int i = 0;
            if (string.IsNullOrEmpty(FileExtension))
            {
                FileExtension = "sql";
            }
            try
            {
                SaveFileDialog save = new SaveFileDialog();
                if (save.ShowDialog() == DialogResult.OK)
                {
                    //判断是否存在后缀
                    i = save.FileName.IndexOf(".");
                    if (i < 0)
                    {
                        i = 0;
                    }
                    pathHZ = save.FileName.Substring(i, 1);
                    if (pathHZ != ".")
                    {
                        tmppath = save.FileName + "."+FileExtension;
                    }
                    else
                    {
                        tmppath = save.FileName;
                    }
                  
                    if (System.IO.File.Exists(tmppath))//检查文件是否存在
                    {
                        DialogResult result = MessageBox.Show("是否覆盖？", "确定", MessageBoxButtons.YesNo);
                        if (result == DialogResult.Yes)
                        {
                            userpath = tmppath;
                        }
                        if (result == DialogResult.No)
                        {
                            userpath = null;
                        }
                    }
                    else
                    {
                        userpath = tmppath;
                    }



                    FileStream fileStream = new FileStream(userpath, FileMode.Create);
                    StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.Default);
                    streamWriter.Write(str + "\r\n");
                    streamWriter.Flush();
                    streamWriter.Close();
                    fileStream.Close();

                }     

            }
            catch (IOException e)
            {
                Console.WriteLine(e.ToString());
                return e.ToString();
            }
            return "保存" + userpath + "成功!";
        }

        public static string readtxt()
        {

            OpenFileDialog opendlg = new OpenFileDialog();
            string sqltxt = string.Empty;
            opendlg.Title = "打开文件";
            //opendlg.InitialDirectory = @"C:\";
            opendlg.Filter = "文件(*.sql)|*.sql|All files(*.*)|*.*";
            try
            {
                if (opendlg.ShowDialog() == DialogResult.OK)
                {
                    FileStream fs = new FileStream(opendlg.FileName, FileMode.Open, FileAccess.Read);
                    StreamReader sr = new StreamReader(fs, Encoding.Default);
                    string sqllinetxt = sr.ReadLine();
                    sqltxt = sqltxt + sqllinetxt;
                    StringBuilder sb = new StringBuilder(500);

                    while (sqllinetxt != null)
                    {
                        sb.Append(sqllinetxt);
                        sqllinetxt = sr.ReadLine();
                        sqltxt = sqltxt + sqllinetxt;
                    }

                    fs.Close();
                    sr.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return e.ToString();
            }

            return get_uft8(sqltxt);
        }

        public static string get_uft8(string unicodeString)
        {
            UTF8Encoding utf8 = new UTF8Encoding();
            Byte[] encodedBytes = utf8.GetBytes(unicodeString);
            String decodedString = utf8.GetString(encodedBytes);
            return decodedString;
        }
    }
}
